﻿/*  
 *  IIS LogFile Analyser, Copyright (C) 2013. http://www.iis-analyser.com
 *  --
 *  This program is free software, provided "AS IS" WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
 * 
 *  File Author: Ian Quigley,    2013-02-23
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using LogParser.DTO;

namespace LogParser.Parsers
{
    internal class LineParser : IParser
    {
        int IParser.Priority { get { return 2; } }

        private DocumentHeader _header;

        internal LineParser( DocumentHeader header )
        {
            _header = header;
        }

        bool IParser.SwitchTo( StringParser parser )
        {
            return !parser.IsEmpty;
        }

        IDocumentLine IParser.Read( StringParser parser )
        {
            List<string> parts = parser.SplitLine( ' ' ).ToList();
            if (parts.Count != _header.Fields.Count)
                return new ReadError("Expected " + _header.Fields.Count + " fields but read " + parts.Count);

            var lineDoc = LineDocument.Build( parts, _header );

            return lineDoc;
        }
    }
}
